package com.capri.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.capri.R;
import com.capri.app.NurseHomeApp;
import com.capri.base.BaseActivity;
import com.capri.model.INurse;
import com.capri.model.bean.BaseResultBean;
import com.capri.model.bean.LoginResultBean;
import com.capri.model.impl.NurseModel;
import com.capri.utils.CommonSubscriber;
import com.capri.utils.PreferenceUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;
import rx.functions.Action0;

/**
 * 登录画面
 */
public class LoginActivity extends BaseActivity {

    @BindView(R.id.edt_phone_num)
    EditText edtPhoneNum;
    @BindView(R.id.edt_password)
    EditText edtPassword;
    @BindView(R.id.tv_forget_password)
    TextView tvForgetPassword;
    @BindView(R.id.tv_modify_password)
    TextView tvModifyPassword;
    @BindView(R.id.tv_login)
    TextView tvLogin;

    private INurse model;

    @Override
    public int getLayoutId() {
        return R.layout.layout_login;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        model = new NurseModel();
    }

    @Override
    public void setupData() {

    }

    @OnClick({R.id.tv_login, R.id.tv_forget_password, R.id.tv_modify_password})
    public void OnClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_login:
                validate();
                break;
            case R.id.tv_forget_password:
                Intent intent = new Intent(LoginActivity.this, ForgetPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_modify_password:
                Intent intent2 = new Intent(LoginActivity.this, ModifyPasswordActivity.class);
                startActivity(intent2);
                break;

        }
    }

    private void validate() {
        if (TextUtils.isEmpty(edtPhoneNum.getText().toString())) {
            Toasty.error(this, "请输入手机号", Toast.LENGTH_SHORT, true).show();
            return;
        }
        if (TextUtils.isEmpty(edtPassword.getText().toString())) {
            Toasty.error(this, "请输入密码", Toast.LENGTH_SHORT, true).show();
            return;
        }
        doLogin();
    }

    private void goToModifyPassword() {

    }

    private void doLogin() {
        model.login(edtPhoneNum.getText().toString(), edtPassword.getText().toString()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                showDialog();
            }
        }).subscribe(new CommonSubscriber<LoginResultBean>() {
            @Override
            public void onSuccess(LoginResultBean response) {
                hideDialog();
                PreferenceUtils.SaveUserToken(response.getToken());
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                NurseHomeApp.getInstance().finishActivity(LoginActivity.this);
                LoginActivity.this.finish();
            }

            @Override
            public void onFailure(BaseResultBean response) {
                hideDialog();
                Toasty.error(LoginActivity.this, response.getMsg(), Toast.LENGTH_SHORT, true).show();
            }
        });
    }
}
